import numpy as np
from econwidgets import (linear_widget,
system_widget,
quadratic_widget,
logarithm_widget,
exponential_widget,
inverse_widget,
quadratic_slope_widget,
derivative_widget,
multivar_widget,
matrix_widget,
taylor_widget,
utility_widget,
elasticities_widget,
slope_from_elasticities_widget,
tax_widget,
uk_IO_widget,
total_revenue_widget,
compound_interest_widget,
log_demand_widget,
profit_maximisation_widget,
plot_income_widget,
plot_dist_widget,
plot_hist_widget,
throw_dice_widget,
tree_widget,
birthday_widget,
norm_area_widget,
norm_binom_approx_widget,
clt_illustration_widget,
bias_variance_widget,
t_area_widget,
hypothesis_test_widget,
error_type_widget,
error_tradeoff_widget,
scatter_plot_widget)
linear_widget(xMax_init = 16, yMax_init = 16, a_init = 0, b_init = 0, Aflag_init = True,
xA_init = -3, yA_init = -14, Bflag_init = True, xB_init = 5, yB_init = 10,
eqFlag1_init = False, eqFlag2_init = False)
Draws a System of two linear equations (e.g. demand/supply)
system_widget(xMax_init = 15, yMax_init = 15, a_init = 4, b_init = 6, c_init = 38,
d_init = -2, e_init = 2, f_init = -4)
Draws a quadratic $ax^2 + bx+c$ given parameters $a,b,c$
quadratic_widget()
Draws a logarithm plot given a choice of base
logarithm_widget()
Draws an exponential plot given a choice of base
exponential_widget(xMax_init = 10, yMax_init = 1000, bases_init = ['2','None','None'])
Draws an exponential and logarithm plot given a choice of base, to illustrate the fact they are each other's inverse
inverse_widget()
Illustrate the calculation of slopes for non-linear functions in a quadratic setting
quadratic_slope_widget()
Illustrates the first and second-order derivatives of a cubic, including turning points
derivative_widget(xMin_init = -3, xMax_init = 3, yMin_init = -10, yMax_init = 10, areaFlag_init = True)
3D plot of a bivariate function (sines, exponentials, true maximum/minimum, 2 different saddle points)
multivar_widget(elev_init = 40, azim_init = 50, yCut_init = True)
3D plot of a bivariate function (sines, exponentials, true maximum/minimum, 2 different saddle points)
multivar_widget(elev_init = 40, azim_init = 50, yCut_init = True)
Geometric illustration of 2x2 matrices as transformations of 2D space
matrix_widget(A_base = [[0.7071,0.7071],[-0.7071,0.7071]], B_base = [[1,1.25],[0,1]],mode_init = 'AB')
Taylor expansion plot for sines, exponetial and logarithms.
taylor_widget(xMin_init = -2, xMax_init = 6, yMin_init = -4, yMax_init = 4, func_init = 'Logarithm', numTerms_init = 18)
utility_widget(a_init = 0.3, elev_init = 15, azim_init = 260, zCut_init = True, constraintCut_init = True)
Linear supply/demand diagram with elasticity calculation
elasticities_widget(Qmax_init = 15, Pmax_init = 30, Qval_init = 6, a_d_init = 0.5,
b_d_init = 22, a_s_init = 0.75, b_s_init = 10, Dflag_init = True, Sflag_init = True)
Supply/demand diagram showing approximate supply/demand around equilibrium points given elasticities
slope_from_elasticities_widget()
Supply/demand diagram showing the impact of a flat tax, including tax revenue from producers/consumers and deadweight loss. Useful for showing how share of the tax burden depends on relative elasticities.
tax_widget(Qmax_init = 10, Pmax_init = 30, Tval_init = 10, a_d_init = 1.5,
b_d_init = 25, a_s_init = 35, b_s_init = -175, Rflag_init = True, Lflag_init = True)
Matrix-based analysis of shock propagation through the UK economiy, based on the ONS' 2017 input-output supply and use tables for the UK.
uk_IO_widget(shockSector_init = ['Financial and insurance', 'Professional and support activities'],
shockSign_init = 'Negative', shockSize_init = 25, NumRounds_init = 5, Normalise_init = False,
plotType_init = 'Shocks over time')
uk_IO_widget(shockSector_init = ['Financial and insurance', 'Professional and support activities'],
shockSign_init = 'Negative', shockSize_init = 25, NumRounds_init = 5, Normalise_init = False,
plotType_init = 'Cumulative shocks')
plots total revenue as a quadratic for a linear demand curve
total_revenue_widget()
illustrates how compounding interest over shorter time-periods convergest to the exponential
compound_interest_widget(numTermsStr_init = '1,4,10',expFlag_init = True)
illustrates how isoelastic functions, which produce non-linear demand functions, are actually linear in logarithmic space
log_demand_widget()
illustrates profit maximisation using both the zero derivative of profit and using marginal revenue / marginal cost.
profit_maximisation_widget()
Plots the empirical distribution of UK household income and provides descriptive statistics. Requires a CSV file containing the income brackets and counts (FY 2017 and 2020 provided)
plot_income_widget(loc = 'assets/income2017.csv')
plot_dist_widget(
dist_1_init = 'Normal', d1_p1_init = 3, d1_p2_init = 0.5, d1_frmt_init = 'r-',
dist_2_init = 'Lognormal', d2_p1_init = 1, d2_p2_init = 0.4, d2_frmt_init = 'g--',
stats_init = False)
Plots a histogram of data, requires a CSV file with raw observations.
plot_hist_widget(data_loc = 'assets/marks.csv', class_init = '0,40,60,70,100',x_label = 'Final Grades',
title = 'Histogram of grades by classification' )
Plots a bar chart for frequencies of N throws of 3 different dice, only one of which is fair. Useful for showing how slowly empirical frequencies converge to probabilities.
throw_dice_widget(1,100) # (a,b) -> Throw dice a b times. 3 dice available
Shows a tree representatinon of sequential events, useful for explaining Bayes rule in a practical setting, where we want the probability ofthe first event having only observed the second.
tree_widget("[ ['S','H',0.001] , ['P','N',[0.99,0.01]] ]", popSize_init = 1000000)
Shows a visual representatinon of the birthday problem, i.e. the probability that a in group of people no birthday is shared. This probability falls to 0 much faster than one might naively expect (k=23 already gives you a 50/50 chance).
birthday_widget(k_init=19)
Shows how to calculate probabilities for a normally-distributed variate as areas under the pdf of an arbitrary normal distribution. Setting mu_init = 0 and sig2_init = 1 provides a standard normal.
norm_area_widget(mu_init=60, sig2_init=49, X_cond_init = '49 < X < 51')
Illustrates the convergence of the binomial distribution $B(n,p)$ to a normal distribution $N(np,np(1-p))$ for large $n$.
norm_binom_approx_widget(n_init = 15, p_init = 0.4, show_norm_init = True)
Illustrates the central limit theorem by calculating the mean or sum of $N$ samples of size $n$ taken from a candidate distribution. The candidate is plotted on the left, the MC frequencies/theoretical Normal distribution is plotted on the right.
clt_illustration_widget(dist_init = 'Exponential', p1_init = 0.1, p2_init = 0,
n_init=100, N_init = 10000, normalise_init = True)
Illustrates the central limit theorem by calculating the mean or sum of $N$ samples of size $n$ taken from a candidate distribution. The candidate is plotted on the left, the MC frequencies/theoretical Normal distribution are plotted on the right.
clt_illustration_widget(dist_init = 'Exponential', p1_init = 0.1, p2_init = 0,
n_init=100, N_init = 10000, normalise_init = True)
Illustrates the differences in bias/variance provided by various estimators of the sample mean, by calculating the mean and median of $N$ samples of size $n$ taken from a candidate distribution. The candidate is plotted on the left, the MC frequenciesare plotted on the right.
bias_variance_widget(dist_init = 'Normal', p1_init = 50, p2_init = 25, n_init=10, N_init = 20000)
Shows the probabilities provided by a Student's T distribution compared to standard normal for a given sample size $n$ and Z-score. Useful for showing how standard normal confidence intervals are overconfident in small samples.
t_area_widget(nu_init=3, frmt_init='b-', X_cond_init = '-1.96 < Z < 1.96')
Plots the accept/reject regions and decision information for a hypothesis test given the sampling distribution and the alternate hypothesis. Provides test statistic and critical value, can handle one/two tailed tests and automatically uses the student T if $n<30$.
hypothesis_test_widget(n_init = 8, H0_init=120, Xbar_init=105, sig2_init=16**2,
tails_init='mu ~= 120', alpha_init = 0.95, t_init=False)
Basic illustration of type I and type II errors based on a noisy scatter plot. Helpful to illustrate (a) the origin of theypeI/II errors when uncertainty is present and (b) that changing the decision threshold to reduce one type of error MUST increase the other type of error.
error_type_widget(noise_init = 0.25, gap_init = 0, threshold_init = 0.2, N_init = 5000)
A more traditional illustration of typeI/II error trade-offs. Note that this widget illustrates the tradeoff for one-tailed tests as well as two tailed, especially the fact that when picking a one-tailed test the extra power in the test comes at the cost of a total loss of power in the direction we assume cannot happen.
error_tradeoff_widget(muH0_init = 0, muH1_init=3, muH2_init=0, sig2_init=1,
Zcrit_init = 1.645, useH2_init = False, twotail_init=False)
error_tradeoff_widget(muH0_init = 0, muH1_init=3, muH2_init=-3.5, sig2_init=1,
Zcrit_init = 1.96, useH2_init = True, twotail_init=True)
Bivariate scatter plot, accepts list of numpy vectors (with option to add normaql noise) or a path to a csv file. Options include display of the correlation and its significance, and a regression line with manual or automatic OLS parameters.
X = np.random.rand(500)
scatter_plot_widget(data = [X,-0.5*X], add_noise_init = 0.1, x_label = r'Variable $X$', y_label = r'Variable $Y$',
rho_init = True, regr_init=True, ols_init = True)